﻿namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
{
    using EnvDTE;
    using EnvDTE80;
    using Microsoft.VisualStudio.Silverlight;
    using Microsoft.VisualStudio.TemplateWizard;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;

    public class RiaServicesLibraryWizard : IWizard
    {
        private SolutionFolder _activeSolutionFolder;
        private DTE2 _dte2;
        private Dictionary<string, string> _replacementsDictionary;
        private Project _slClassLibProject;

        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        public void ProjectFinishedGenerating(Project project)
        {
            this._slClassLibProject = project;
        }

        public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
        }

        public void RunFinished()
        {
            Solution2 solution = (Solution2) this._dte2.Solution;
            string fullName = this._slClassLibProject.FullName;
            string name = this._replacementsDictionary["$safeprojectname$"];
            ProjectItem parentProjectItem = this._slClassLibProject.ParentProjectItem;
            ProjectItems items = (parentProjectItem == null) ? null : parentProjectItem.Collection;
            Project project = (items == null) ? null : (items.Parent as Project);
            SolutionFolder folder = ((project != null) && (project.Kind == "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}")) ? (project.Object as SolutionFolder) : null;
            SolutionFolder folder2 = folder ?? this._activeSolutionFolder;
            if (folder2 == null)
            {
                try
                {
                    folder2 = (SolutionFolder) solution.AddSolutionFolder(name).Object;
                }
                catch (COMException)
                {
                    folder2 = null;
                }
            }
            string language = this._slClassLibProject.CodeModel.Language.Equals("{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}", StringComparison.OrdinalIgnoreCase) ? "VisualBasic" : "CSharp";
            string projectTemplate = solution.GetProjectTemplate("ClassLibrary.zip", language);
            string str5 = name + ".Web";
            string destination = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(fullName)), str5);
            if (folder2 != null)
            {
                folder2.AddFromTemplate(projectTemplate, destination, str5);
                if (folder == null)
                {
                    solution.Remove(this._slClassLibProject);
                    this._slClassLibProject = folder2.AddFromFile(fullName);
                }
            }
            else
            {
                solution.AddFromTemplate(projectTemplate, destination, str5, false);
            }
            SLProjectExtender extender = this._slClassLibProject.get_Extender("SilverlightProject") as SLProjectExtender;
            if (extender != null)
            {
                string extension = Path.GetExtension(fullName);
                extender.LinkedServerProject = Path.Combine("..", Path.Combine(str5, str5 + extension));
            }
        }

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            this._dte2 = (DTE2) automationObject;
            this._replacementsDictionary = replacementsDictionary;
            this._replacementsDictionary["$targetsilverlightversion$"] = TemplateUtilities.GetSilverlightToolsVersion();
            this._dte2.Globals["safeclientprojectname"] = replacementsDictionary["$safeprojectname$"];
            Array source = null;
            try
            {
                source = (Array) this._dte2.ActiveSolutionProjects;
            }
            catch (COMException)
            {
            }
            Project project = (source == null) ? null : source.OfType<Project>().FirstOrDefault<Project>();
            if ((project != null) && (project.Kind == "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}"))
            {
                this._activeSolutionFolder = project.Object as SolutionFolder;
            }
        }

        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
    }
}

